package com.redis.config;


import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    public void onMessage(Message message, byte[] pattern) {
        String expiredKey = message.toString(); // 获取过期的key
        System.out.println(expiredKey);
        if (expiredKey.contains("TIME_KEY")) { // 判断是否是想要监听的过期key
            System.out.println(expiredKey);
        }
    }

    @Override
    protected void doRegister(RedisMessageListenerContainer listenerContainer) {
        super.doRegister(listenerContainer);
    }
}
